package com.h3c.web.rest;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.h3c.dal.common.TableConstant;
import com.h3c.dal.dao.CasssoResourceDAO;
import com.h3c.dal.dao.CasssoRoleResourceRelationDAO;
import com.h3c.dal.entity.CasssoResourceDO;
import com.h3c.dal.entity.CasssoRoleResourceRelationDO;
import com.h3c.dal.entity.query.CasssoResourceQuery;
import com.h3c.dal.entity.query.CasssoRoleResourceRelationQuery;
import com.h3c.web.common.BaseResponse;
import com.h3c.web.common.ErrorMsgEnum;
import com.h3c.web.common.constant.SessionConstant;
import com.h3c.web.common.utils.BeanUtil;
import com.h3c.web.common.utils.ListUtil;
import com.h3c.web.common.vo.CasssoResourceVO;

/**
 * @ClassName: ResRest
 * @Description: 资源权限api
 * @author: d14287
 * @date: 2017年5月31日 下午3:46:50
 */
@Controller
@RequestMapping("console/res")
public class ResRest {
	public static final Logger logger = LoggerFactory.getLogger(ResRest.class);
	@Autowired
	private CasssoRoleResourceRelationDAO casssoRoleResourceRelationDAO;
	@Autowired
	private CasssoResourceDAO casssoResourceDAO;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/queryMenu.ajax", method = RequestMethod.GET)
	public @ResponseBody BaseResponse queryMenu(HttpServletRequest request) throws Exception {
		String roleId = (String) request.getSession().getAttribute(SessionConstant.ROLE_ID);
		CasssoRoleResourceRelationQuery roleResQuery = new CasssoRoleResourceRelationQuery();
		roleResQuery.createCriteria().andRoleIdEqualTo(roleId).andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
		List<CasssoRoleResourceRelationDO> roleResDOList = casssoRoleResourceRelationDAO
				.selectByExample(roleResQuery);
		if (ListUtil.isEmpty(roleResDOList)) {
			return BaseResponse.falseOf(ErrorMsgEnum.res_not_exist.getMsg());
		}
		List<CasssoResourceVO> resVOList = Lists.newArrayList();
		roleResDOList.forEach(roleRes -> {
			CasssoResourceQuery resQuery = new CasssoResourceQuery();
			resQuery.createCriteria().andResIdEqualTo(roleRes.getResId())
					.andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL)
					.andResTypeEqualTo(TableConstant.RESOURCE_TYPE_MENU);
			List<CasssoResourceDO> resDOList = casssoResourceDAO.selectByExample(resQuery);
			if (resDOList != null && !resDOList.isEmpty()) {
				CasssoResourceVO resVO = new CasssoResourceVO();
				try {
					BeanUtil.copyProperties(resDOList.get(0), resVO);
				} catch (Exception e) {
					logger.error("queryMenu happend error",e);
				}
				resVOList.add(resVO);
			}
		});
		return BaseResponse.trueOf(resVOList);
	}

	/**
	 * 结构化菜单
	 * 
	 * @return
	 */
	public void treeMenu(List<CasssoResourceVO> resList) {
		// 目前只有一级菜单
		Collections.sort(resList, new Comparator<CasssoResourceVO>() {
			@Override
			public int compare(CasssoResourceVO vo1, CasssoResourceVO vo2) {
				if (vo1.getOrderNum() == vo2.getOrderNum())
					return 0;
				if (vo1.getOrderNum() > vo2.getOrderNum())
					return 1;
				return -1;
			}
		});
	}
}
